<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div id="ex1">
    <p class="p1"></p>
    <p class="p2"></p>
    <p class="p3"></p>
    <p class="huge"></p>
  </div>
</body>
<style>
  #ex1{
    position: relative;
  }
#ex1 p {
  position: sticky;
  top: 10px;
}
#ex1 .p1 {
  background-color: blue;
  width: 20px;
  height: 20px;
  z-index: 2;
}
#ex1 .p2{
  background-color: red;
  width: 40px;
  height: 40px;
  z-index: 1;
}
#ex1 .p3{
  background-color: green;
  width: 80px;
  height: 80px;
}
#ex1 .huge{
  height: 2000px;
}
</style>
</html>